//选项卡
function tab(option){
	var _self = this,
		_option = option,
		_checkInterface = function(o){
			_self.trigger = o.trigger ? $(o.trigger) : null;
			_self.pannel = o.pannel ? $(o.pannel) : null;
			if(_self.trigger == null || _self.pannel == null){
				return false;
			}
			_self.eventType = o.eventType || "click";
			_self.toTarget = o.toTarget || 0;
			_self.trigCurClass = o.trigCurClass || "current";
			_self.callback = o.callback || function(){};
			_self.mouseDelay = o.mouseDelay || 0;
			if(o.lazyloadImg && o.lazyloadImg["node"]){
				_self.lazyloadImg = {};
				_self.lazyloadImg["node"] = o.lazyloadImg["node"];
				_self.lazyloadImg["sourceImg"] = o.lazyloadImg["sourceImg"] || "imgsrc";
				_self.lazyloadImg["open"] = true;
				_self.lazyloadImg["data"] = {};
			}
			return true;
		};
	if(_checkInterface(_option)){
		_init();
	}
	function _init(){
		var mouseTimer;
		if(_self.lazyloadImg && _self.lazyloadImg["open"]){
			_self.switchTo(_self.toTarget);
		}
		if(_self.eventType == "click"){
			_self.trigger.bind({
				click : function(e){
					_self.switchTo($(this).index());
				}
			});
		}
		else if(_self.eventType == "mouse"){
			_self.trigger.bind({
				mouseenter : function(e){
					var index = $(this).index();
					if(_self.mouseDelay != 0){
						mouseTimer = setTimeout(function(){
							_self.switchTo(index);
						},_self.mouseDelay);
					}
					else{
						clearTimeout(mouseTimer);
						_self.switchTo(index);
					}
				}
			});
			if(_self.mouseDelay != 0){
				_self.trigger.bind({
					mouseleave : function(e){
						clearTimeout(mouseTimer);
					}
				});
			}
		}
	}
}
tab.prototype = {
	switchTo : function(target){
		var _self = this,
			curPanel = _self.pannel.eq(target);
		_self.trigger.removeClass(_self.trigCurClass);
		_self.trigger.eq(target).addClass(_self.trigCurClass);
		_self.pannel.css({"display":"none"});
		curPanel.css({"display":"block"});
		if(_self.lazyloadImg && _self.lazyloadImg["open"]){
			_self.lazyload({target : target,curPanel : curPanel},_self.lazyloadImg);
		}
		_self.callback.call(_self,target,curPanel);
	},
	lazyload : function(baseOption,lazyOption){
		var _self = this,
			lazyData = _self.lazyloadImg["data"],
			_target = baseOption.target,
			_panel,
			_node,
			_sourceImg;
		for(var prop in lazyData){
			if(prop == _target){
				return;
			}
		}
		_panel = baseOption.curPanel;
		_node = _panel.find(lazyOption.node);
		_target = baseOption.target;
		$(_node).each(function(i){
			$(this).attr("src",function(){
				_sourceImg = $(this).attr(lazyOption.sourceImg);
				$(this).removeAttr(lazyOption.sourceImg);
				return _sourceImg;
			});
			_self.lazyloadImg["data"][_target] = true;

		});
	}
}
//轮播图
function slide_wall(option){
	var that = this;
	that.wrap = $(option.wrap);
	that.pic = that.wrap.find(option.pic);
	that.trig = that.wrap.find(option.trig);
	that.len = that.trig.length;
	that.index = 0;
	that.effectTime = 300;
	that.playTime = option.playTime || 3000;
	that.autoPlay = true;
	that.isFade = option.isFade || false;
	that.trigType = option.trigType || "mouse";
	that.trigCurStyle = option.trigCurStyle || "normal";
	that.trig.each(function(i){
		if(that.trigType == "mouse"){
			$(this).bind({
				mouseenter:function(){
					that.switchTo(i);
				}
			});
		}
		else if(that.trigType == "click"){
			$(this).bind({
				click:function(){
					that.switchTo(i);
				}
			});
		}
	});
	if(that.autoPlay){
		that.wrap.bind({
			mouseover:function(){
				clearInterval(that.intervalTimer);
			},
			mouseout:function(){
				that.autoPlayFn();
			}
		});
		that.autoPlayFn();

	}
}
slide_wall.prototype = {
	switchTo:function(index){
		var that = this;
		if(index < that.len){
			if(index < 0){
				that.index = that.len - 1;
			}
			else{
				that.index = index;
			}
		}
		else{
			that.index = 0;
		}
		if(!that.isFade){
			that.pic.css({"display":"none"});
			that.trig.removeClass("current");
			that.pic.eq(that.index).css({"display":"block"});
			that.trig.eq(that.index).addClass("current");
		}
		else{
		that.trig.removeClass("current");
		that.trig.eq(that.index).addClass("current");
		that.pic.removeClass("current");
		that.pic.stop(true,true).animate({opacity:"hide"},that.effectTime);
		that.pic.eq(that.index).addClass("current").stop(true,true).animate({opacity:"show"},that.effectTime);
		}
		if(that.trigCurStyle == "slide"){
			that.slide_current = that.wrap.find(".slide_current");
			that.slide_current_len = 50;
			that.slide_current.stop(true,true).animate({
				"top": that.slide_current_len * that.index
			},that.effectTime);
		}
	},
	autoPlayFn:function(){
		var that = this;
		that.intervalTimer = setInterval(function(){
			that.index++;
			that.switchTo(that.index);
		},that.playTime);
	}
}